from src.crud import (sys_dict_header as sysDictHeaderCrud,
                      sys_dict_table as sysDictTableCrud,
                      sys_dict_value as sysDictValueCrud
                      )
from src.schemas import (sys_dict_header as sysDictHeaderSchema,
                         sys_dict_table as sysDictTableSchema,
                         sys_dict_value as sysDictValueSchema
                         )
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from src.core.ormdb import get_db
from src.schemas import request_model
from src.schemas.login_manager import AuthManager
from src.core import dependencies, retMes

router = APIRouter(
    # dependencies=[Depends(dependencies.auth_token)]  # ← 全局生效
    # prefix="/user",
    # tags=["sys_user"]  # ,
    # dependencies=[Depends(get_token_header)],
    # responses={404: {"description": "Not found"}},
    # description="Operations related to system users",
    # on_startup=[startup_event],
    # on_shutdown=[shutdown_event]
)


# region ====================== 字典表头 ======================
@router.post("/dict-headers/batch", summary="批量新增字典表头", response_model=request_model.ResponseModel)
def create_dict_headers(
        items: list[sysDictHeaderSchema.SysDictHeaderCreate],
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return sysDictHeaderCrud.create_dict_headers(db, auth, items)


@router.post("/dict-headers", summary="单条新增字典表头")
def create_dict_header(
        item: sysDictHeaderSchema.SysDictHeaderCreate,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return sysDictHeaderCrud.create_dict_header(db, auth, item)


@router.get("/dict-headers/{id}", response_model=request_model.ResponseModel)
async def read_user(
        id: str,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    # 按 id 查单条
    return sysDictHeaderCrud.get_dict_headers(db,
                                              auth,
                                              None,
                                              None,
                                              None,
                                              id)


@router.get("/dict-headers", summary="分页查询字典表头", response_model=request_model.ResponseModel)
def read_dict_headers(
        page: int = 1,
        page_size: int = 100,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return sysDictHeaderCrud.get_dict_headers(db, auth, page, page_size)


@router.post("/dict-headers/query", summary="条件查询字典表头", response_model=request_model.ResponseModel)
def query_dict_headers(
        query: request_model.QueryModel | None = None,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return sysDictHeaderCrud.get_dict_headers(db,
                                              auth,
                                              None,
                                              None,
                                              query)


@router.put("/dict-headers/{pk}", summary="单条更新字典表头")
def update_dict_header(
        pk: str,
        item: sysDictHeaderSchema.SysDictHeader,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return sysDictHeaderCrud.update_dict_header(db, auth, pk, item)


@router.put("/dict-headers", summary="批量更新字典表头")
def update_dict_headers(
        model: request_model.UpdateModel,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return sysDictHeaderCrud.update_dict_headers(db, auth, model)


@router.delete("/dict-headers", summary="删除字典表头", response_model=request_model.ResponseModel)
def delete_dict_headers(
        model: request_model.DeleteModel,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return sysDictHeaderCrud.delete_dict_headers(db, auth, model)


# endregion

# region ====================== 字典表模式 ======================
@router.post("/dict-tables/batch", summary="批量新增字典表模式", response_model=request_model.ResponseModel)
def create_dict_tables(
        items: list[sysDictTableSchema.SysDictTableCreate],
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return sysDictTableCrud.create_dict_tables(db, auth, items)


@router.post("/dict-tables", summary="单条新增字典表模式", response_model=request_model.ResponseModel)
def create_dict_table(
        item: sysDictTableSchema.SysDictTableCreate,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return sysDictTableCrud.create_dict_table(db, auth, item)


@router.get("/dict-tables", summary="分页查询字典表模式", response_model=request_model.ResponseModel)
def read_dict_tables(
        page: int = 1,
        page_size: int = 100,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return sysDictTableCrud.get_dict_tables(db, auth, page, page_size)


@router.post("/dict-tables/query", summary="条件查询字典表模式", response_model=request_model.ResponseModel)
def query_dict_tables(
        query: request_model.QueryModel | None = None,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return sysDictTableCrud.get_dict_tables(db,
                                            auth,
                                            None,
                                            None,
                                            query)


@router.put("/dict-tables/{pk}", summary="单条更新字典表模式")
def update_dict_table(
        pk: str,
        item: sysDictTableSchema.SysDictTable,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return sysDictTableCrud.update_dict_table(db, auth, pk, item)


@router.put("/dict-tables", summary="批量更新字典表模式", response_model=request_model.ResponseModel)
def update_dict_tables(
        model: request_model.UpdateModel,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return sysDictTableCrud.update_dict_tables(db, auth, model)


@router.delete("/dict-tables", summary="删除字典表模式")
def delete_dict_tables(
        model: request_model.DeleteModel,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return sysDictTableCrud.delete_dict_tables(db, auth, model)


# endregion

# region ====================== 字典值 ======================
@router.post("/dict-values/batch", summary="批量新增字典值", response_model=request_model.ResponseModel)
def create_dict_values(
        items: list[sysDictValueSchema.SysDictValueCreate],
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return sysDictValueCrud.create_dict_values(db, auth, items)


@router.post("/dict-values", summary="单条新增字典值", response_model=request_model.ResponseModel)
def create_dict_value(
        item: sysDictValueSchema.SysDictValueCreate,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return sysDictValueCrud.create_dict_value(db, auth, item)


@router.get("/dict-values", summary="分页查询字典值", response_model=request_model.ResponseModel)
def read_dict_values(
        page: int = 1,
        page_size: int = 100,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return sysDictValueCrud.get_dict_values(db, auth, page, page_size)


@router.post("/dict-values/query", summary="条件查询字典值", response_model=request_model.ResponseModel)
def query_dict_values(
        query: request_model.QueryModel | None = None,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return sysDictValueCrud.get_dict_values(db,
                                            auth,
                                            None,
                                            None,
                                            query)


@router.put("/dict-values/{pk}", summary="单条更新字典值", response_model=request_model.ResponseModel)
def update_dict_value(
        pk: str,
        item: sysDictValueSchema.SysDictValue,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return sysDictValueCrud.update_dict_value(db, auth, pk, item)


@router.put("/dict-values", summary="批量更新字典值", response_model=request_model.ResponseModel)
def update_dict_values(
        model: request_model.UpdateModel,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return sysDictValueCrud.update_dict_values(db, auth, model)


@router.delete("/dict-values", summary="删除字典值", response_model=request_model.ResponseModel)
def delete_dict_values(
        model: request_model.DeleteModel,
        db: Session = Depends(get_db),
        auth: AuthManager = Depends(dependencies.auth_token)
):
    return sysDictValueCrud.delete_dict_values(db, auth, model)
# endregion
